<?php
/**
 * Created by PhpStorm.
 * User: long
 * Date: 2016/9/25
 * Time: 14:05
 */

namespace Notify\Controller;


use Common\Model\LessonModel;
use Common\Model\LessonOrderModel;
use Common\Model\MemberModel;
use Common\Model\MemberOutModel;
use Common\Model\MemChainModel;
use Common\Model\OrderModel;
use Common\Model\ProductModel;
use Common\Model\PublicMoneyModel;
use Common\Model\ScoreModel;
use Common\Model\SystemMsgModel;
use Common\Model\WalletBillModel;
use Common\Model\WalletModel;
use System\Model\SmsModel;
use Think\Log;


/**
 * 微信支付
 * Class WxpayController
 * @package Notify\Controller
 */
class WxpayController extends NotifybaseController
{

    public $order_model;
    public $smslog_model;
    public $system_msg_model;

    public $member_model;
    public $lesson_order_model;
    public $lesson_model;
    public $product_model;


    public function __construct()
    {
        parent::__construct();
        $this->smslog_model         = new SmsModel();
        $this->order_model          = new OrderModel();
        $this->system_msg_model     = new SystemMsgModel();
        $this->member_model         = new MemberModel();
        $this->product_model        = new ProductModel();

        header("Content-type:text/html;charset=utf-8");
        ini_set('date.timezone', 'Asia/Shanghai');
        vendor('WxPayPubHelper.WxPayPubHelper');
    }

    public function index()
    {
        Log::record(json_encode(file_get_contents('php://input')), Log::WARN);
        //使用通用通知接口
        $notify = new \Notify_pub();
        //存储微信的回调
//        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $xml = file_get_contents('php://input');

        //回调错误
        if (!$xml) {
            Log::record('微信支付回调校验失败1:' . json_encode($xml), Log::WARN);
            return false;
        }

        $notify->saveData($xml);
        //签名状态
        $checkSign = true;
        //验证签名，并回应微信。
        //对后台通知交互时，如果微信收到商户的应答不是成功或超时，微信认为通知失败，
        //微信会通过一定的策略（如30分钟共8次）定期重新发起通知，
        //尽可能提高通知的成功率，但微信不保证通知最终能成功。
        if ($notify->checkSign() == FALSE) {
            $notify->setReturnParameter("return_code", "FAIL");//返回状态码
            $notify->setReturnParameter("return_msg", "签名失败");//返回信息
            $checkSign = false;
        } else {
            $notify->setReturnParameter("return_code", "SUCCESS");//设置返回码
        }
        $returnXml = $notify->returnXml();



        if (!$checkSign) {
            Log::record('微信支付回调校验失败2:' . json_encode($xml), Log::WARN);
            exit;
        }

        //通知微信，成功获取到相应的异步通知
        echo $returnXml;

        //微信返回参数
        $back_data = $notify->getData();

        $order_sn = $back_data['out_trade_no']; //订单号


        $order = $this->order_model->where(['order_sn' => $order_sn])->find();//dump($order);
        if (!$order) {
            Log::record($order_sn . '订单编号不存在', Log::WARN);
            exit('fail');
        }

        $time = date('Y-m-d H:i:s');
        $data = [
            'status'        => OrderModel::STATUS_PAY_SUCCESS,
            'pay_type'      => OrderModel::PAY_TYPE_WX,
            'pay_time'      => $time,
            'back_data'     => json_encode($back_data),
            'update_time'   => $time,
        ];

        $result = $this->order_model->where(['order_sn' => $order_sn])->save($data);
        $order_id = $this->order_model->where(['order_sn' => $order_sn])->getField('id');
        $this->product_model->changeStock($order_id);
        $msg = $this->system_msg_model->addOne($order['mid'], '您的订单已支付成功，请耐心等待发货');
    }

}